import format from "date-fns/format";

/**
 * 工具函数
 */
export function getUrlParams() {
  const url = window.location.href;
  if ("undefined" == url) {
    return {};
  }
  let paraString = url.substring(url.indexOf("?") + 1, url.length).split("&");
  let paraObj = {};
  if (paraString == url) {
    return "";
  }
  for (let i = 0; i < paraString.length; i++) {
    let j = paraString[i];
    paraObj[j.substring(0, j.indexOf("="))] = j.substring(
      j.indexOf("=") + 1,
      j.length
    );
  }
  return paraObj;
}

//时间转换
export function timeFormat(timestamp, reg) {
  var result;
  const yesterdayStart = getSeconds(getYesterday() + " " + "00:00:00"); //昨天开始时间
  const yesterdayEnd = getSeconds(getYesterday() + " " + "23:59:59"); //昨天结束时间
  const beforeyesterdayStart = getSeconds(beforeYesterday() + " " + "00:00:00"); //前天开始时间
  const beforeyesterdayEnd = getSeconds(beforeYesterday() + " " + "23:59:59"); //前天结束时间
  if (timestamp > yesterdayEnd) {
    result = "今天 " + format(new Date(timestamp), "HH:mm");
  } else if (timestamp >= yesterdayStart && timestamp <= yesterdayEnd) {
    result = "昨天 " + format(new Date(timestamp), "HH:mm");
  } else if (
    timestamp >= beforeyesterdayStart &&
    timestamp <= beforeyesterdayEnd
  ) {
    result = "前天 " + format(new Date(timestamp), "HH:mm");
  } else {
    result = format(new Date(timestamp), reg ? reg : "YYYY-MM-DD HH:mm");
  }
  return result;
}

//获取昨天
function getYesterday() {
  var day1 = new Date();
  day1.setTime(day1.getTime() - 24 * 60 * 60 * 1000);
  var s1 =
    day1.getFullYear() + "-" + (day1.getMonth() + 1) + "-" + day1.getDate();
  return s1;
}

//获取前天天
function beforeYesterday() {
  var day1 = new Date();
  day1.setTime(day1.getTime() - 24 * 60 * 60 * 1000 * 2);
  var s1 =
    day1.getFullYear() + "-" + (day1.getMonth() + 1) + "-" + day1.getDate();
  return s1;
}
//日期获取毫秒数
function getSeconds(date) {
  var time = new Date(date.replace(/-/g, "/"));
  var seconds = time.getTime();
  return seconds;
}

export function imageHandler(imgPath, e) {
  console.log(imgPath);
  const img = e.srcElement;
  img.src = imgPath;
}
